package com.jiaai.shop.jpush;


import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.jiaai.shop.util.ThreadPoolProxy;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author
 * @version V1.0
 * @title jpush
 * @since 2017/11/16
 */
public class JPushUtils {
    public  String key;
    public  String secet;
    public ThreadPoolProxy threadPoolProxy;

    public JPushUtils(String key, String secet, ThreadPoolProxy threadPoolProxy) {
        this.key = key;
        this.secet = secet;
        this.threadPoolProxy = threadPoolProxy;
    }

    /**
     * 推送消息给所有平台匹配标签的用户
     * @param title
     * @param msg
     * @param alias
     */
    public void pushMsgByAllByAlias(String title, Message msg, String ... alias){
        threadPoolProxy.commitTask(new JPushThread(key,secet,buildPushObject_all_alias(title,msg,alias)));
    }

    /**
     * 构建所有平台按标签的结构体
     * @return
     */
    private PushPayload buildPushObject_all_alias(String title, Message msg, String ...alias) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.alert(title))
                .setMessage(msg)
                .build();

    }


}
